/*
 * JerboJournalExpiryBox.java
 *
 * Created on May 8, 2008, 5:06 PM
 */

package jerbo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Action;

/**
 *
 * @author  luigi
 */
public class JerboJournalExpiryBox extends javax.swing.JDialog {
    
    public static boolean INPUT_DATE = true;
    public static boolean SELECT_DATE = false;
    
    private boolean mode = SELECT_DATE;
    
    private java.util.ResourceBundle appBundle = 
            java.util.ResourceBundle.getBundle("jerbo/resources/JerboApp"); // NOI18N
    
    private SimpleDateFormat dateFormatTxt = new SimpleDateFormat(
            appBundle.getString("Application.date.format"));
    private SimpleDateFormat dateFormatDb = new SimpleDateFormat("yyyyMMdd");
    
    
    private Integer result_date = null;
    
    /** Creates new form JerboJournalExpiryBox
     * @param parent
     * @param mode 
     */
    public JerboJournalExpiryBox(java.awt.Frame parent, boolean mode) {
        super(parent);
        initComponents();
        setMode(mode);
    }
    
    public void setMode(boolean mode) {
        this.mode = mode;
        if (mode == SELECT_DATE) {
            dateText.setVisible(false);
            dateComboBox.setVisible(true);
            noneButton.setEnabled(false);
        } else if (mode == INPUT_DATE) {
            dateText.setVisible(true);
            dateComboBox.setVisible(false);
            noneButton.setEnabled(true);
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        backPanel = new javax.swing.JPanel();
        try {
            javax.swing.text.MaskFormatter mf = new javax.swing.text.MaskFormatter(appBundle.getString("Application.date.mask"));
            mf.setPlaceholderCharacter('0');
            dateText = new javax.swing.JFormattedTextField(mf);
        } catch (java.text.ParseException pEx) {
            pEx.printStackTrace();
        }
        dateLabel = new javax.swing.JLabel();
        dateComboBox = new javax.swing.JComboBox();
        cancelButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        noneButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(jerbo.JerboApp.class).getContext().getResourceMap(JerboJournalExpiryBox.class);
        setTitle(resourceMap.getString("title")); // NOI18N
        setModal(true);
        setName("Form"); // NOI18N
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        backPanel.setName("backPanel"); // NOI18N

        dateText.setName("dateText"); // NOI18N

        dateLabel.setText(resourceMap.getString("dateLabel.text")); // NOI18N
        dateLabel.setName("dateLabel"); // NOI18N

        dateComboBox.setBackground(new java.awt.Color(255, 255, 255));
        dateComboBox.setName("dateComboBox"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(jerbo.JerboApp.class).getContext().getActionMap(JerboJournalExpiryBox.class, this);
        cancelButton.setAction(actionMap.get("cancel")); // NOI18N
        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N

        okButton.setAction(actionMap.get("ok")); // NOI18N
        okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
        okButton.setName("okButton"); // NOI18N

        noneButton.setAction(actionMap.get("none")); // NOI18N
        noneButton.setText(resourceMap.getString("noneButton.text")); // NOI18N
        noneButton.setName("noneButton"); // NOI18N

        org.jdesktop.layout.GroupLayout backPanelLayout = new org.jdesktop.layout.GroupLayout(backPanel);
        backPanel.setLayout(backPanelLayout);
        backPanelLayout.setHorizontalGroup(
            backPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(backPanelLayout.createSequentialGroup()
                .addContainerGap(16, Short.MAX_VALUE)
                .add(backPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, backPanelLayout.createSequentialGroup()
                        .add(noneButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 90, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(okButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 90, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cancelButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 90, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, backPanelLayout.createSequentialGroup()
                        .add(backPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(dateLabel)
                            .add(dateComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 190, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(dateText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 170, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(54, 54, 54))))
        );
        backPanelLayout.setVerticalGroup(
            backPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(backPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(dateLabel)
                .add(6, 6, 6)
                .add(backPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(dateComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(dateText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(12, 12, 12)
                .add(backPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(okButton)
                    .add(noneButton)
                    .add(cancelButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE))
                .addContainerGap())
        );

        getContentPane().add(backPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 310, 100));

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    @Action
    public void none() {
        this.result_date = 0;
        this.dispose();
    }

    
    @Action
    public void cancel() {
        this.result_date = null;
        this.dispose();
    }
    
    @Action
    public void ok() {
        Date date = null;
        if (mode == SELECT_DATE) {
            try {
                date = dateFormatTxt.parse((String) dateComboBox.getSelectedItem());
            } catch (ParseException ex) {
                Logger.getLogger(JerboJournalExpiryBox.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            try {
                date = dateFormatTxt.parse((String) dateText.getText());
            } catch (ParseException ex) {
                Logger.getLogger(JerboJournalExpiryBox.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        if (date != null)
            this.result_date = Integer.valueOf(dateFormatDb.format(date));
        else
            this.result_date = null;
        
        this.dispose();
    }
    
    public void setDates(Vector dates) {
        dateComboBox.removeAllItems();
        if (mode == SELECT_DATE) {
            try {
                for (int i = 0; i < dates.size(); i++) {
                    dateComboBox.addItem(dateFormatTxt.format(dateFormatDb.parse((String) dates.get(i))));
                }
            } catch (ParseException ex) {
                Logger.getLogger(JerboJournalExpiryBox.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    public Integer getResultDate() throws ParseException {
        return this.result_date;
    }
        
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel backPanel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JComboBox dateComboBox;
    private javax.swing.JLabel dateLabel;
    private javax.swing.JFormattedTextField dateText;
    private javax.swing.JButton noneButton;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables
    
}
